/*
 *   Copyright (C) 2011 David Swords
 *	
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package format;

public class NumberConverter {

	private final String class_details = "NumberConverter.";
	
	private void chk(final boolean expression, final String message, final String location) {
		if(expression) {
			System.err.println("#############################################################");
			System.err.println(message);
			System.err.println(location);
			System.err.println("#############################################################");
			System.exit(1);
		}
	}
	
	public int hexToDec(String hex) {
		 return Integer.parseInt(hex, 16);
	}
	
	public String decToHex(int dec) {
		return Integer.toHexString(dec);
	}
	
	public double systemToDirectionRadians(final int system) {
		final String method_details = "systemToRadians(int) returns double";
		
		int temp_system = system;
		
		if(system >= 63735 && system <= 65535) {
			temp_system -= 63735;
		}
		
		if(system >= 0 && system <= 1800) {
			temp_system += 1800;
		}
	
		return (2 * Math.PI * temp_system) / 3600;
	}
	
	public double systemToDirection(final int system) {
		final String method_details = "systemToRadians(int) returns double";
		
		int temp_system = system;
		
		if(system >= 63735 && system <= 65535) {
			temp_system -= 63735;
		}
		
		if(system >= 0 && system <= 1800) {
			temp_system += 1800;
		}
	
		return temp_system;
	}
	
	public int radianToFlipperSystem(final double radian) {
		final String method_details = class_details + "systemToFlipperRadians(int) returns double";
		chk(!(radian >= 0 && radian <= 2*Math.PI), "The double radian range is invalid. 0 to 2*PI only, it was " + radian, method_details);
		int temp_system = (int)((65535 * radian) / (2*Math.PI));
		chk(!(temp_system >= 0 && temp_system <= 65535), "The double temp_system is invalid. 0 to 65,535 only, it was " + temp_system, method_details);
		return temp_system;
	}
	
	public double systemToDegrees(int system) {
		if(system > 10000) {
			return (((2*Math.PI)*(system - 62000)) / 3534) * (180 / Math.PI);
		} else {
			return (((2*Math.PI)*(system)) / 3534) * (180 / Math.PI);
		}
	}
	
	public double angleToRadians(int angle) {
		return angle / 180;
	}
	
	public String convertHexToString(String hex){
		 
		  StringBuilder sb = new StringBuilder();
		  StringBuilder temp = new StringBuilder();
	 
		  for( int i=0; i < hex.length() - 1; i += 2 ) {
	 
		      String output = hex.substring(i, (i + 2));
		      int decimal = Integer.parseInt(output, 16);
		      sb.append((char)decimal);
		      temp.append(decimal);
		  }
		  
		  System.out.println("Decimal : " + sb.toString());
	 
		  return sb.toString();
	  }
}
